package com.haojiang.mh.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.haojiang.mh.R;

/**
 * 通用Dialog
 */
public class CommonDialog extends Dialog {

    public CommonDialog(@NonNull Context context) {
        super(context);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    public CommonDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    public static class Builder {
        private CommonDialog dialog;
        private Context ctx;//上下文
        private TextView tvTitle, tvContent, tvLeft, tvRight;//标题、内容、左边按妞、右边按钮
        private OnClickListener leftClickListener, rightClickListener;
        private boolean mCancelable = false, mCanceledOnTouchOutside = false;//是否可以触摸外侧消失
        private boolean isSelfControl = false;//是否单独控制
        private int type;//1:双按钮   2:单按钮
        private String title, leftText, rightText;
        private String content;//内容文本

        public Builder(Context context, int type) {
            this.ctx = context;
            this.type = type;
        }

        public CommonDialog create() {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new CommonDialog(ctx, R.style.CommonDialogStyle);
            assert inflater != null;
            View rootView = inflater.inflate(R.layout.layout_common_dialog, null);
            dialog.addContentView(rootView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            View contentView = null;
            switch (type) {
                case 1:
                    contentView = rootView.findViewById(R.id.layout_type_1);
                    tvTitle = contentView.findViewById(R.id.tv_title_1);
                    tvContent = contentView.findViewById(R.id.tv_content_1);
                    tvLeft = contentView.findViewById(R.id.tv_left_1);
                    tvRight = contentView.findViewById(R.id.tv_right_1);
                    if (!TextUtils.isEmpty(title)) tvTitle.setText(title);
                    if (!TextUtils.isEmpty(leftText)) tvLeft.setText(leftText);
                    if (!TextUtils.isEmpty(rightText)) tvRight.setText(rightText);
                    tvContent.setText(content);
                    break;
                case 2:
                    contentView = rootView.findViewById(R.id.layout_type_2);
                    tvTitle = contentView.findViewById(R.id.tv_title_2);
                    tvContent = contentView.findViewById(R.id.tv_content_2);
                    tvLeft = contentView.findViewById(R.id.tv_left_2);
                    if (!TextUtils.isEmpty(leftText)) tvLeft.setText(leftText);
                    if (!TextUtils.isEmpty(title)) tvTitle.setText(title);
                    tvContent.setText(content);
                    break;
            }
            if (contentView == null) return null;
            contentView.setVisibility(View.VISIBLE);
            if (tvLeft != null) {
                tvLeft.setOnClickListener(v -> {
                    if (leftClickListener != null) {
                        leftClickListener.onClick(dialog, BUTTON_NEGATIVE);
                    }
                    if (isSelfControl) return;
                    dialog.dismiss();
                });
            }
            if (tvRight != null) {
                tvRight.setOnClickListener(v -> {
                    if (rightClickListener != null) {
                        rightClickListener.onClick(dialog, BUTTON_NEGATIVE);
                    }
                    if (isSelfControl) return;
                    dialog.dismiss();
                });
            }
            dialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside);
            dialog.setCancelable(mCancelable);
            return dialog;
        }

        /**
         * 设定Title和Content文本
         */
        public Builder setTitleContent(String title, String content) {
            this.title = title;
            this.content = content;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        /**
         * 设定两个按钮点击监听
         */
        public Builder setLeftRightClick(OnClickListener leftListener, OnClickListener rightListener) {
            this.leftClickListener = leftListener;
            this.rightClickListener = rightListener;
            return this;
        }

        /**
         * 设定单个按钮的情况
         */
        public Builder setLeftClick(OnClickListener leftListener) {
            this.leftClickListener = leftListener;
            return this;
        }

        /**
         * 设定单个按钮的情况
         */
        public Builder setLeftClick(OnClickListener leftListener, String button) {
            this.leftClickListener = leftListener;
            this.leftText = button;
            return this;
        }

        /**
         * 设定单个按钮的情况
         */
        public Builder setRightClick(OnClickListener rightListener) {
            this.rightClickListener = rightListener;
            return this;
        }

        /**
         * 设定左右按钮的文本
         */
        public Builder setLeftRightText(String left, String right) {
            leftText = left;
            rightText = right;
            return this;
        }

        public CommonDialog getDialog() {
            return dialog;
        }

        /**
         * 设置触摸其他地方取消dialog
         */
        public Builder setCanceledOnTouchOtherPlace(boolean canceledOnTouchOtherPlace) {
            this.mCanceledOnTouchOutside = canceledOnTouchOtherPlace;
            return this;
        }

        /**
         * 设置返回键取消dialog
         */
        public Builder setCancelable(boolean cancelable) {
            this.mCancelable = cancelable;
            return this;
        }

        /**
         * 自我控制点击事件
         */
        public Builder setSelfControl(boolean selfControl) {
            this.isSelfControl = selfControl;
            return this;
        }
    }
}
